/*
 * @Author: Starfish_DL
 * @Date: 2020-10-19 22:16:21
 * @LastEditors: Starfish_DL
 * @LastEditTime: 2020-10-21 21:37:48
 * @CurrentVersion: 1.0
 * @Description: 
 */

#include<iostream>
#include<string>
#define MAXSIZE 50
using namespace std;

class charNode
{
public:
    char data;
    charNode *next;
    charNode();
    charNode(char a);
    charNode(char a,charNode* next);
    ~charNode();
};

charNode::charNode()
{
    this->data='\0';
    this->next=nullptr;
}

charNode::charNode(char a)
{
    this->data=a;
    this->next=nullptr;
}

charNode::charNode(char a,charNode *next)
{
    this->data=a;
    this->next=next;
}

charNode::~charNode()
{
}

class cLinkList
{
private:
    charNode *head=new charNode();
public:
    cLinkList(string str,unsigned int n);
    cLinkList(string str);
    cLinkList();
    void cLStrCopy(string str);
    void cLStrCopy(cLinkList str);
    bool cLStrEqual(cLinkList str);
    int cLStrLength();
    void cLConcat(string str);
    void cLConcat(cLinkList str);
    string cLSubStr(unsigned int a,unsigned int b);
    void cLInsStr(unsigned int i,string str);
    void cLInsStr(unsigned int i,cLinkList str);
    string cLDelStr(unsigned int a,unsigned int b);
    void cLRepStr(unsigned int a,unsigned int b,string str);
    void cLRepStr(unsigned int a,unsigned int b,cLinkList str);
    void cLDispStr();
    cLinkList operator+(const cLinkList &str);
    ~cLinkList();
};

cLinkList::cLinkList(string str,unsigned int n)
{
    int i=0;
    charNode *p=this->head;
    charNode *pre;
    while (i<n && str[i]!='\0')
    {
        pre=new charNode(str[i]);
        p->next=pre;
        p=pre;
        i++;
    }
}

cLinkList::cLinkList(string str)
{
    int i=0;
    charNode *p=this->head;
    charNode *pre;
    while (str[i]!='\0')
    {
        pre=new charNode(str[i]);
        p->next=pre;
        p=pre;
        i++;
    }
}

cLinkList::cLinkList()
{
}

cLinkList::~cLinkList()
{
    charNode* p=this->head;
    charNode* pre=p->next;
    while (pre!=nullptr)
    {
        delete p;
        p=pre;
        pre=p->next;
    }
    delete p;
}

void cLinkList::cLConcat(string str)
{
    charNode *p=this->head;
    while (p->next!=nullptr)
    {
        p=p->next;
    }
    int i=0;
    charNode *pre;
    while (str[i]!='\0')
    {
        pre=new charNode(str[i]);
        p->next=pre;
        p=pre;
        i++;
    }
}

void cLinkList::cLConcat(cLinkList str)
{
    charNode *p=this->head;
    while (p->next!=nullptr)
    {
        p=p->next;
    }
    charNode *n=str.head->next;
    charNode *pre;
    while (n!=nullptr)
    {
        pre=new charNode(n->data);
        n=n->next;
        p->next=pre;
        p=pre;
    }
}

string cLinkList::cLDelStr(unsigned int a,unsigned int b)
{
    int i=1;
    charNode *p=this->head;
    while (i<a)
    {
        p=p->next;
        i++;
    }
    charNode *pre=p->next;
    charNode *temp=p;
    string t="";
    while (i<=b)
    {
        p=pre;
        pre=pre->next;
        t.append(1,p->data);
        delete p;
        i++;
    }
    temp->next=pre;
    return t;
}

void cLinkList::cLDispStr()
{
    charNode *p=this->head->next;
    while (p!=nullptr)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
}

void cLinkList::cLInsStr(unsigned int i,string str)
{
    charNode *n=this->head;
    for (int k = 1; k < i; k++)
    {
        n=n->next;
    }
    int j=0;
    charNode *pre=new charNode(str[j]);
    charNode *t=pre;
    charNode *p;
    while (str[j+1]!='\0')
    {
        j++;
        p=pre;
        pre=new charNode(str[j]);
        p->next=pre;
    }
    pre->next=n->next;
    n->next=t;

    // star 可以使用以下方法简化代码
    // cLinkList l(str);
    // this->cLInsStr(i,l);
}

void cLinkList::cLInsStr(unsigned int i,cLinkList str)
{
    charNode *n=this->head;
    for (int k = 1; k < i; k++)
    {
        n=n->next;
    }
    charNode *p=str.head;
    while (p->next!=nullptr)
    {
        p=p->next;
    }
    p->next=n->next;
    n->next=str.head->next;
    delete str.head;
}

void cLinkList::cLRepStr(unsigned int a,unsigned int b,string str)
{
    this->cLDelStr(a,b);
    this->cLInsStr(a,str);
}

void cLinkList::cLRepStr(unsigned int a,unsigned int b,cLinkList str)
{
    this->cLDelStr(a,b);
    this->cLInsStr(a,str);
}

void cLinkList::cLStrCopy(cLinkList str)
{
    charNode* p=this->head;
    charNode* pre=p->next;
    while (pre->next!=nullptr)
    {
        p=pre;
        pre=pre->next;
        delete p;
    }
    delete pre;
    this->head->next=str.head->next;
}

bool cLinkList::cLStrEqual(cLinkList str)
{
    charNode *p=this->head->next;
    charNode *n=str.head->next;
    while (p!=nullptr && n!=nullptr)
    {
        if (p->data!=n->data)
        {
            return false;
        }
        p=p->next;
        n=n->next;
    }
    if (p==nullptr && n==nullptr)
    {
        return true;
    }
    return false;
}

int cLinkList::cLStrLength()
{
    charNode *p=this->head;
    int i=0;
    while (p!=nullptr)
    {
        p=p->next;
        i++;
    }
    return i;
}

cLinkList cLinkList::operator+(const cLinkList &str)
{
    cLinkList l=(*this);
    l.cLConcat(str);
    return l;
}

int main()
{
    string str1="16h889";
    string str2="177ggh";
    cLinkList c1(str1);
    c1.cLDispStr();
    cLinkList c2(str2);
    c2=c2+c1;
    c2.cLDispStr();
    return 0;
}